//
// Created by 66477 on 2022/9/28.
//

#ifndef DATA_STRUCTURE_QUEUE4_3_H
#define DATA_STRUCTURE_QUEUE4_3_H

#include "Node4_3.h"

class Queue4_3 {
private:
    Node4_3 *pointer = nullptr;
public:
    Queue4_3() {
        pointer = new Node4_3();
        pointer->setNext(pointer);
    }

    void free() {
        if (pointer->getNext() == pointer) {
            return;
        }
        Node4_3 *p = pointer->getNext()->getNext(), *q;
        pointer = pointer->getNext();
        while (p != pointer) {
            q = p;
            p = p->getNext();
            delete q;
        }
        pointer->setNext(pointer);
    }

    void push(char c) {
        Node4_3 *n = new Node4_3(c), *p = pointer->getNext();
        pointer->setNext(n);
        n->setNext(p);
        pointer = n;
    }

    void pop() {
        if (pointer->getNext() == pointer) {
            cout << "Queue are empty!" << endl;
            return;
        }
        Node4_3 *p = pointer->getNext()->getNext();
        pointer->getNext()->setNext(p->getNext());
        if (pointer->getNext()->getNext() == pointer) {
            pointer = pointer->getNext();
        }
        delete p;
    }

    void print() {
        if (pointer == pointer->getNext()) {
            cout << "Queue are empty!" << endl;
            return;
        }
        Node4_3 *p = pointer->getNext();
        do {
            p = p->getNext();
            cout << p->getData() << " ";
        } while (p != pointer);
        cout << endl;
    }

};

#endif //DATA_STRUCTURE_QUEUE4_3_H
